package com.ttg.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.ttg.config.Properties;
import com.ttg.contants.KeyContants;
import com.ttg.data.InitGatewayReqData;
import com.ttg.pojo.QueryOrderReqDTO;
import com.ttg.security.SecurityKit;
import com.ttg.utils.HttpKit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

//~--- classes ----------------------------------------------------------------

/**
 * Class GwQueryOrderDemoController
 * Description 说明：查询订单接口测试DEMO
 * Create 2016-08-09 13:38:18
 * @author Ardy
 */
@Controller
@RequestMapping
public class GwQueryOrderDemoController {

    /**
     * Field LOGGER
     * Description
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(GwQueryOrderDemoController.class);

    /**
     * Method testQueryOrderDemo
     * Description 说明：查询订单
     *
     * @return 返回值说明：
     */
    @RequestMapping("/query_order")
    @ResponseBody
    public ModelAndView testQueryOrderDemo() {
        QueryOrderReqDTO queryOrderReq = InitGatewayReqData.initQueryOrderReq();
        ModelAndView     modelAndView  = new ModelAndView();
        String           url           = "req_query_order";

        modelAndView.addObject("url", url);
        modelAndView.setViewName("demo/query_order_req_demo");
        modelAndView.addObject("queryOrder", queryOrderReq);

        return modelAndView;
    }

    /**
     * Method reqQueryOrder
     * Description 说明：
     *
     * @param merchantId 说明：
     * @param merOrderId 说明：
     * @param oriMerOrderId 说明：
     * @param oriTransDate 说明：
     *
     * @return 返回值说明：
     */
    @RequestMapping(
        value    = "/req_query_order",
        method   = RequestMethod.POST
    )
    public ModelAndView reqQueryOrder(@RequestParam(
        value    = "merchantId",
        required = true
    ) String merchantId, @RequestParam(
        value    = "merOrderId",
        required = true
    ) String merOrderId, @RequestParam(
        value    = "oriMerOrderId",
        required = true
    ) String oriMerOrderId, @RequestParam(
        value    = "oriTransDate",
        required = true
    ) String oriTransDate) {
        QueryOrderReqDTO queryOrderReq = new QueryOrderReqDTO();
        queryOrderReq.setMerchantId(merchantId);
        queryOrderReq.setMerOrderId(merOrderId);
        queryOrderReq.setOriMerOrderId(oriMerOrderId);
        queryOrderReq.setOriTransDate(oriTransDate);

        queryOrderReq.setSignatureAlgorithm(KeyContants.SECURITY_TYPE);
        queryOrderReq.setSignatureInfo(
            SecurityKit.signByRSA(HttpKit.httpGwParam2SignStr(queryOrderReq.getAllParameters())));

        ModelAndView modelAndView = new ModelAndView();
        String       url          = Properties.URL.getString("GATEWAY_BASE_SCHEME")
                                    + Properties.URL.getString("QUERY_ORDER_URL");

        modelAndView.addObject("url", url);
        modelAndView.setViewName("req/query_order_demo");
        modelAndView.addObject("queryOrder", queryOrderReq);

        return modelAndView;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
